<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>订单链接测试</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; }
        .test-section { margin: 20px 0; padding: 15px; border: 1px solid #ccc; }
        .success { color: green; }
        .error { color: red; }
        .info { color: blue; }
        button { margin: 5px; padding: 10px; }
        .test-btn { padding: 8px 12px; background: #007bff; color: white; border: none; border-radius: 4px; cursor: pointer; }
        .test-btn:hover { background: #0056b3; }
    </style>
</head>
<body>
    <h1>订单链接功能测试</h1>
    
    <div class="test-section">
        <h3>1. 模拟用户登录</h3>
        <button onclick="simulateLogin()">模拟Mike登录</button>
        <div id="loginStatus"></div>
    </div>
    
    <div class="test-section">
        <h3>2. 测试订单链接</h3>
        <button class="test-btn" onclick="testOrdersLink()">测试我的订单链接</button>
        <button class="test-btn" onclick="openOrdersPage()">直接打开订单页面</button>
        <div id="linkTestStatus"></div>
    </div>
    
    <div class="test-section">
        <h3>3. 用户状态</h3>
        <div id="userStatus"></div>
    </div>
    
    <div class="test-section">
        <h3>4. 控制台日志</h3>
        <div id="consoleLog"></div>
    </div>

    <!-- 引入必要的脚本 -->
    <script src="js/api.js"></script>
    <script src="js/auth.js"></script>
    <script src="js/main.js"></script>
    
    <script>
        // 重写console.log来显示在页面上
        const originalLog = console.log;
        const originalWarn = console.warn;
        const originalError = console.error;
        
        function addToConsole(message, type = 'info') {
            const logDiv = document.getElementById('consoleLog');
            const timestamp = new Date().toLocaleTimeString();
            logDiv.innerHTML += `<div class="${type}">[${timestamp}] ${message}</div>`;
            logDiv.scrollTop = logDiv.scrollHeight;
        }
        
        console.log = function(...args) {
            originalLog.apply(console, args);
            addToConsole(args.join(' '), 'info');
        };
        
        console.warn = function(...args) {
            originalWarn.apply(console, args);
            addToConsole(args.join(' '), 'error');
        };
        
        console.error = function(...args) {
            originalError.apply(console, args);
            addToConsole(args.join(' '), 'error');
        };
        
        // 模拟用户登录
        function simulateLogin() {
            const statusDiv = document.getElementById('loginStatus');
            try {
                const userData = {
                    username: 'Mike',
                    user_id: 'USER001',
                    email: 'mike@example.com',
                    phone: '1234567890',
                    user_type: 'Customer',
                    token: 'test-token-mike'
                };
                
                localStorage.setItem('currentUser', JSON.stringify(userData));
                localStorage.setItem('userData', JSON.stringify(userData));
                localStorage.setItem('authToken', userData.token);
                
                if (window.authManager) {
                    window.authManager.currentUser = userData;
                    window.authManager.isLoggedIn = true;
                    window.authManager.userType = userData.user_type;
                    window.authManager.updateUI();
                }
                
                statusDiv.innerHTML = '<div class="success">✓ Mike用户登录成功</div>';
                console.log('Mike用户登录成功:', userData);
                updateUserStatus();
            } catch (error) {
                statusDiv.innerHTML = '<div class="error">✗ 登录失败: ' + error.message + '</div>';
                console.error('登录失败:', error);
            }
        }
        
        // 测试订单链接
        function testOrdersLink() {
            const statusDiv = document.getElementById('linkTestStatus');
            try {
                if (window.authManager && window.authManager.goToOrders) {
                    console.log('调用goToOrders方法...');
                    window.authManager.goToOrders();
                    statusDiv.innerHTML = '<div class="success">✓ 订单链接测试成功</div>';
                } else {
                    statusDiv.innerHTML = '<div class="error">✗ goToOrders方法不存在</div>';
                    console.error('goToOrders方法不存在');
                }
            } catch (error) {
                statusDiv.innerHTML = '<div class="error">✗ 测试失败: ' + error.message + '</div>';
                console.error('测试失败:', error);
            }
        }
        
        // 直接打开订单页面
        function openOrdersPage() {
            console.log('直接打开订单页面...');
            window.open('orders.html', '_blank');
        }
        
        // 更新用户状态显示
        function updateUserStatus() {
            const statusDiv = document.getElementById('userStatus');
            const userData = JSON.parse(localStorage.getItem('currentUser') || '{}');
            
            if (userData.username) {
                statusDiv.innerHTML = `
                    <div class="success">
                        <strong>当前用户:</strong> ${userData.username}<br>
                        <strong>用户类型:</strong> ${userData.user_type}<br>
                        <strong>登录状态:</strong> 已登录
                    </div>
                `;
            } else {
                statusDiv.innerHTML = '<div class="error">未登录</div>';
            }
        }
        
        // 页面加载完成后运行测试
        window.addEventListener('load', () => {
            console.log('页面加载完成，订单链接测试准备就绪');
            
            // 检查模块加载状态
            setTimeout(() => {
                console.log('模块加载状态:');
                console.log('- API:', !!window.api);
                console.log('- AuthManager:', !!window.authManager);
                console.log('- MainApp:', !!window.mainApp);
                
                updateUserStatus();
            }, 100);
        });
    </script>
</body>
</html>
